package com.espirit.builder.biz;

import java.util.List;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.I18n;
import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.manager.workflow.WFCondition;
import com.espirit.eap.manager.workflow.WFPhase;
import com.espirit.eap.pagelayout.Element;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.GridSection;
import com.espirit.eap.pagelayout.ListLayout;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.pagelayout.TabbedSection;
@Component
public class WFPhaseBuilder extends ModelBuilderSupport {
	private static final String FIELD_NAME="name";
	private static final String FIELD_DESCRIPTION = "description";
	private static final String FIELD_USERS ="users";
	private static final String FIELD_NEEDACCEPTUSERS ="needAcceptUsers";
	private static final String FIELD_WFCONDITION="wfCondition";
	
	@Override
	public boolean init() {
		return super.setClass(WFPhase.class, "Workflow Phase", "批准阶段") ;
	}

	@Override
	public void buildFields() {
		addModelField(FIELD_NAME, "Phase Name", "阶段名称");
		addModelField(FIELD_DESCRIPTION, "Description", "描述");
		addModelField(FIELD_USERS, "Users", "用户");
		addModelField(FIELD_NEEDACCEPTUSERS, "Need Accept Users", "需要的批准人数");
		addModelField(FIELD_WFCONDITION,"Condtion","条件");
	}
	
	@Override
	public void prepareLayoutElements() {
		ModelField mfName = getModelField(FIELD_NAME);
		ModelField mfDescription = getModelField(FIELD_DESCRIPTION);
		
		addListField(mfName,mfDescription);
		
		addDetailField(mfName,mfDescription,getModelField(FIELD_NEEDACCEPTUSERS));
		setRequired(FIELD_NAME);
		
		super.prepareLayoutElements();
	}
	
	@Override
	protected Integer getDetailLayoutSectionColsPerRow() {
		return 1;
	}
	
	@Override
	protected void afterBuilderLayout() {
		ListLayout ll = createListLayout(model, "WFPhase-Users");
		ll.setDefaulted(false);
		GridSection gs = ll.createGridSection();
		gs.setReadOnly(false);
		gs.setTagName("datagrid");
		gs.setNamePrefix(FIELD_USERS);
		
		elementService.addSectionElement(gs, convertFieldToElement(getModelField(FIELD_USERS), true));
		updateObject(model);
		ll = getListLayout(model.getName(), "WFPhase-Users");

		List<Element> wfConditionChildren = getModel(WFCondition.class).getDefaultDetailLayout().getSortChildren();
		TabbedSection ts = elementService.createTabbedSection(getDefaultDetailLayout(), 2);
		FlatSection fsUsers = ts.getTabPage(0);
		
		I18n i18n = i18nService.addI18n("参与用户", "Involved Users");
		fsUsers.setI18n(i18n.getKey());
		elementService.createSectionElement(fsUsers, ll.getSortChildren());

		FlatSection tabPage = ts.getTabPage(1);
		
		elementService.createSectionElement(tabPage, wfConditionChildren);
		tabPage.setI18n(getModelField(FIELD_WFCONDITION).getI18n());
		super.afterBuilderLayout();
	}
	
}
